﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Util;

namespace CourseSystem.Common.Helpers
{
    public class UploadAuth : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // base.OnActionExecuting(filterContext);
            string ran = HttpContext.Current.Request["ranStr"].CheckStr("");
            filterContext.Controller.ViewData["ranStr"] = ran;
            if (string.IsNullOrEmpty(ran))
            {
                var ranStr = RandomStr.GetByTime(UserHelper.LoginUser.Id + "_" + (byte)UserHelper.LoginUser.UserType + "_");
                if (string.IsNullOrEmpty(ranStr))
                {
                    filterContext.Result = new ViewResult
                    {
                        ViewName = "NotFound"
                    };
                    return;
                }
                filterContext.Controller.ViewData["ranStr"] = ranStr;
            }
            else
            {
                if (!RandomStr.Check(ran))
                {
                    filterContext.Result = new ViewResult
                    {
                        ViewName = "NotFound"
                    };
                    return;
                }
            }
            base.OnActionExecuting(filterContext);
        }
    }

    public class UploadedAuth : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // base.OnActionExecuting(filterContext);
            string ran = HttpContext.Current.Request["ran"].CheckStr("");
            filterContext.Controller.ViewData["ranStr"] = ran;
            if (!RandomStr.Check(ran))
            {
                filterContext.Result = new ViewResult
                {
                    ViewName = "NotFound"
                };
                return;
            }
            base.OnActionExecuting(filterContext);
        }
    }
}
